package com.example.idea.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Dict;
import com.example.idea.entity.Orders;
import com.example.idea.entity.User;
import com.example.idea.service.OrdersService;
import com.example.idea.service.UserService;
import com.example.idea.utils.HoneyLogs;
import com.example.idea.utils.LogType;
import com.example.idea.utils.Result;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 功能：用户登录验证
 * 作者：妙笔生花
 * 日期：2024/9/4 19:57
 */
@CrossOrigin
@RestController
@RequestMapping("/login")
@Slf4j
public class WebController {

    @Resource
    private UserService userService;
    @Resource
    private OrdersService ordersService;

    @HoneyLogs(operation = "用户",type = LogType.LOGIN)
    @PostMapping("/login")
    public Result login(@RequestBody User user) {
        log.info("用户登录登录:{}", user);
        if (user.getPassword().isEmpty() || user.getUsername().isEmpty()) {
            return Result.error(500,"数据不合法，请检查后在输入");
        }
       user  = userService.login(user);
        return Result.success(user);
    }

    @HoneyLogs(operation = "用户",type = LogType.REGISTER)
    @PostMapping("/ZhuCe")
    public Result register(@RequestBody User user) {
        log.info("用户注册:{}", user);
        if (user.getPassword().isEmpty() || user.getUsername().isEmpty() || user.getRole().isEmpty()) {
            return Result.error(500,"数据不合法，请检查后在输入");
        }
        user = userService.addPlusUser(user);
        return Result.success(user);
    }

    @GetMapping("/charts")
    public Result charts() {
//        包装折线图的数据
        List<Orders> list = ordersService.list();
        Set<String> collect = list.stream().map(Orders::getDate).collect(Collectors.toSet());
        List<String> dateList = CollUtil.newArrayList(collect);
        dateList.sort(Comparator.naturalOrder());
        List<Dict> lineList = new ArrayList<>();
        for (String date : dateList) {
            BigDecimal sum = list.stream().filter(orders -> orders.getDate()
                            .equals(date)).map(Orders::getMoney)
                    .reduce(BigDecimal::add).orElse(BigDecimal.ZERO);
            Dict dict = Dict.create();
            Dict line = dict.set("date",date).set("value",sum);
            lineList.add(line);
        }

        List<Dict> barList = new ArrayList<>();
        Set<String> categories = list.stream().map(Orders::getCategory).collect(Collectors.toSet());
        for (String date : categories) {
            BigDecimal sum = list.stream().filter(orders -> orders.getCategory()
                            .equals(date)).map(Orders::getMoney)
                    .reduce(BigDecimal::add).orElse(BigDecimal.ZERO);
            Dict dict = Dict.create();
            Dict bar = dict.set("category",date).set("value",sum);
            barList.add(bar);
        }

        List<Dict> pieList = new ArrayList<>();
        Set<String> pieDate = list.stream().map(Orders::getCategory).collect(Collectors.toSet());
        for (String date : pieDate) {
            BigDecimal sum = list.stream().filter(orders -> orders.getCategory()
                            .equals(date)).map(Orders::getMoney)
                    .reduce(BigDecimal::add).orElse(BigDecimal.ZERO);
            Dict dict = Dict.create();
            Dict bar = dict.set("name",date).set("value",sum);
            pieList.add(bar);
        }

        Dict res = Dict.create().set("line",lineList).set("bar",barList).set("pie",pieList);
        return Result.success(res);
    }
}
